package com.ls.pattern.behavior.command;

public class RemoteController {
    //开按钮名令
    private Command[] onCommands;
    //关闭名令
    private Command[] offCommands;
    //执行撤销命令
    private Command undoCommands;

    public RemoteController() {
        onCommands = new Command[5];
        offCommands = new Command[5];

        for (int i = 0; i < 5; i++) {
            onCommands[i] = new NoCommand();
            offCommands[i] = new NoCommand();

        }
    }


    //给按钮设置需要的命令
    public void setCommand(int no, Command onCommand, Command offCommand) {
        onCommands[no] = onCommand;
        offCommands[no] = offCommand;
    }


    //按下关按钮
    public void onButtonWasPushed(int no) {
        //找到你按下的开的按钮,并表用对应的方法
        onCommands[no].execute();
        //记录这次操作,用于撤销
        undoCommands = onCommands[no];
    }

    //按下开按钮
    public void offButtonWasPushed(int no) {
        //找到你按下的开的按钮,并表用对应的方法
        offCommands[no].execute();
        //记录这次操作,用于撤销
        undoCommands = offCommands[no];
    }

    //按下撤销按钮
    public void undoButtonWasPushed(int no) {
        undoCommands.undo();

    }

}
